package cn.edu.nju.software.zp13.client.assignment9UsingJAXB.assignment4Refactoring.scoreList1.core;

import java.util.LinkedList;
import java.util.List;

import cn.edu.nju.software.zp13.client.assignment9UsingJAXB.assignment3Refactoring.bean.CourseScore;
import cn.edu.nju.software.zp13.client.assignment9UsingJAXB.assignment3Refactoring.bean.Student;
import cn.edu.nju.software.zp13.client.assignment9UsingJAXB.assignment3Refactoring.bean.Students;
import cn.edu.nju.software.zp13.client.assignment9UsingJAXB.assignment4Refactoring.bean.CourseScoreWithManyScores;
import cn.edu.nju.software.zp13.client.assignment9UsingJAXB.assignment4Refactoring.bean.CourseScoreList;


public class StudentsToCourseScoreList implements Adapter<Students, CourseScoreList>{

	@Override
	public  CourseScoreList tansfer(Students f) {
		CourseScoreList csl=new CourseScoreList();
		List<CourseScoreWithManyScores> courseScores=new LinkedList<>();
		List<Student> students=f.getStudents();
		for(Student s:students){
			CourseScore[] css= s.getCs();
			for(CourseScore cs:css){
				addCourseScoreRecord(courseScores, cs);
			}
		}
		csl.add(courseScores);
		return csl;
	}
	
	private void addCourseScoreRecord(List<CourseScoreWithManyScores> courseScores,CourseScore cs){
		int scoreRecordPos=-1;
		for(int i=0;i<courseScores.size();i++){
			CourseScoreWithManyScores cswms=courseScores.get(i);
			if(cswms.getCourseId().equals(cs.getCourseId())&&cswms.getSt()==cs.getSt()){
				System.out.println("set i");
				scoreRecordPos=i;
			}
		}
		if(scoreRecordPos==-1){
			CourseScoreWithManyScores cswms=new CourseScoreWithManyScores();
			cswms.setCourseId(cs.getCourseId());
			cswms.setSt(cs.getSt());
			cswms.getScore().add(cs.getScore());
			courseScores.add(cswms);
		}else{
			courseScores.get(scoreRecordPos).getScore().add(cs.getScore());
		}
	}
}
